package com.golang.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.golang.api.domain.Community;
import com.golang.api.service.ICommunityService;
import com.golang.api.util.Result;
import com.golang.api.util.ResultGenerator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 社区Controller
 */
@RestController
@RequestMapping("/system/community")
public class CommunityController {

    private final Log logger = LogFactory.getLog(CommunityController.class);

    @Autowired
    private ICommunityService communityService;

    /**
     * 新建社区
     *
     * @param community 社区对象
     * @return 操作结果
     */
    @PostMapping("/create")
    public Result create(Community community) {
        boolean isSaved = communityService.save(community);
        if (isSaved) {
            return ResultGenerator.success("社区创建成功");
        } else {
            return ResultGenerator.failure("社区创建失败");
        }
    }

    /**
     * 获取社区列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize) {
        Page<Community> page = new Page<>(pageNum, pageSize);
        IPage<Community> communityPage = communityService.page(page);
        return ResultGenerator.success(communityPage);
    }
}